<?php

namespace app\index\service;

use think\Db;

/**
 * @author xjy
 */
class CountryskuService
{
    private static $salesState=["1"=>"新产品","2"=>"清仓品","3"=>"在售品","4"=>"下架品"];  //销售状态
    private static $cycleStage=["1"=>"补货期","2"=>"新品期","3"=>"计划淘汰期","4"=>"淘汰期"];  //生命周期
    private static $salesDistributed=["1"=>"爆品","2"=>"畅销款","3"=>"平销款","4"=>"滞销款","5"=>"无动销"];  //生命周期
    //SKU生命周期列表
    static public function getList($param)
    {
        if(empty($param['down'])){
             $titles=[];
        }else{
             $titles=[
                 'country_name'=>'国家名称','sku'=>'SKU','cname'=>'中文名称','sales_distributed_name'=>'动销分布（1=爆品 2=畅销款 3=平销款 4=滞销款 5=无动销）','sales_state_name'=>'销售状态（1-新产品；2-清仓品；在售品；4-下架品）',
                 'sku_grade'=>'SKU等级','is_time_name'=>'季节新产品（1-是，2-否）','seasonal_cycle'=>'季节月份','state'=>'状态'];
        }
        
        return \app\index\model\ProduceModel::getCountrySkuList($param,$titles,"app\index\service\CountryskuService::convertData");  
    }
    public static function convertData(){
       
        $gets=func_get_args();
        if($gets[0]){
            foreach($gets[0] as $key=>&$value){
                $value['sales_state_name']=isset(self::$salesState[$value['sales_state']])?self::$salesState[$value['sales_state']]:'';
                $value['sales_distributed_name']=isset(self::$salesDistributed[$value['sales_distributed']])? self::$salesDistributed[$value['sales_distributed']]:'';
                $value['state']=($value['status']==1) ? "正常":"禁止";
                $value['seasonal_cycle']="'". $value['seasonal_cycle'];
                $value['is_time_name']=($value['is_time']==1) ? "是": (($value['is_time']==2) ? "否":'');
            }
        }
        return $gets[0];  
    }
    //校验批量处理
    static public function uploadHandle($data,$param){
        $msg=[];//提示信息
        $num=0;
        $sku=[];$insertData=[];
        $getCountry=\app\common\ConfigBase::getCountry();
        foreach ($data as $k=>$v){
            if(!empty($v['1'])){
                $sku[]=strtoupper($v[1]);
                $num++;
            } 
        }
        if($num>5000){
             return ["code" => "0", "msg" => '上传不能大于5000'];
        }
        $res=\app\common\service\SkuService::getBatchInfoBySku($sku);
        $skuName=array_column($res,'cnname','sku');
        $title=['SKU','sales_state','sku_grade','sales_distributed','is_time','season_start_time','season_end_time'];
        foreach($data as $key=>$value){
            $one=[];$str='';$str1='';$seasonal_cycle='';
            $line=$key+2;
            $cname = isset($skuName[$value['1']]) ? $skuName[$value['1']]:'';
            if(empty($value['0']) || !in_array($value[0],$getCountry) || empty($value['1']) || empty($cname)){
                if(empty($value['0']) || empty($value['1']) ){
                    $str="国家为空或者SKU为空";
                }else if( !in_array($value[0],$getCountry)){
                    $str="国家{$value['0']}不存在";
                }
                if(empty($value['0']) || empty($value['1']) ){
                    $str1="国家为空或者SKU为空";
                }else if(!empty($value['1']) && empty($cname)){
                    $str1="SKU:{$value['1']}不存在";
                }
                $msg[]="第{$line}行".$str.",".$str1;
            }
            
            if(!empty($value['1'])){
                 $one['sku'] = strtoupper($value['1']);
            }
            if(!empty($cname)){
                 $one['cname'] = $cname;
            }
            if(!empty($value['0'])){
                 $one['country_name'] = $value['0'];
            }
            if(!empty($value['2'])){
                if($value['2']< 1 || $value['2']>4){
                     $msg[]="第{$line}行:销售状态应该在1-4之间";
                }
                 $one['sales_state'] = $value['2'];
            }
            if(!empty($value['3'])){
                 $one['sku_grade'] = $value['3'];
            }
            if(!empty($value['4'])){
                if($value['4']< 1 || $value['4']>5){
                     $msg[]="第{$line}行:动销分布应该在1-5之间";
                }
                 $one['sales_distributed'] = $value['4'];
            }
            $one['is_time'] = 2;
            if(!empty($value['5']) && ($value['5']>0 && $value['5']<3)){
                $one['is_time'] = $value['5'];
            }else if(!empty($value['5']) && ($value['5']>2 || $value['5']<1)){
                $msg[]="第{$line}行:是否是季节性产品不能为空而且只能是1-2";
            }
            if($one['is_time']==1){
                if(!empty($value['6'])){
                    if($value['6']< 1 &&$value['6']>12){
                         $msg[]="第{$line}行:季节开始月份应该在1-12之间";
                    }
                    $seasonal_cycle .= $value['6']; 
                }
                if(!empty($value['7'])){
                    if($value['7']< 1 &&$value['7']>12){
                         $msg[]="第{$line}行:季节结束月份应该在1-12之间";
                    }
                    $seasonal_cycle .= "-".$value['7'];  
                }
                if($value['6']>=$value['7']){
                     $msg[]="第{$line}行:季节开始月份只能小于季节结束月份";
                 }
            } 
            if($one['is_time']==1 && !empty($seasonal_cycle)){
                $one['seasonal_cycle'] = $seasonal_cycle;
            }else{
                $one['seasonal_cycle'] = '';
            }
            
            $one['update_time'] = date('Y-m-d H:i:s');
            $one['operator'] = CN_NAME;
            if(count($msg)==0){
                $insertData[]=$one;
            }
        }
        if($insertData){
             \app\common\model\ToolModel::dbInsertOrUpdate('country_sku_status',$insertData);
        }
        return ["code" => "1", "msg" => join('<br/>',$msg)];
    }
}
